﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySpace.Data;
using DataBaseTable;

public partial class Manage_WishNote_List : System.Web.UI.Page
{
    private static int nowpages;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            nowpages = 1;
            this.DataBd();

        }
    }

    private void DataBd()
    {
        DBHandler dbh = GetHandler.Init();
        dbh.CommandText = "select * from WishingWall order by id desc";
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = dbh.ExecuteDataTable().DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = 20;
        toone.Enabled = true;
        toup.Enabled = true;
        tonext.Enabled = true;
        tolast.Enabled = true;
        if (nowpages == 1)
        {
            toone.Enabled = false;
            toup.Enabled = false;
        }
        if (nowpages == ps.PageCount)
        {
            tonext.Enabled = false;
            tolast.Enabled = false;
        }
        if (!IsPostBack)
        {
            for (int i = 1; i <= ps.PageCount; i++)
            {
                jumppage.Items.Add(i.ToString());
            }
        }
        curpage.Text = nowpages.ToString();
        sumpage.Text = ps.PageCount.ToString();
        tpagesizes.Text = ps.PageSize.ToString();
        pagesum.Text = ps.DataSourceCount.ToString();
        ps.CurrentPageIndex = nowpages - 1;//获取当前页码
        GridView1.DataSource = ps;
        GridView1.DataBind();
    }
    protected void toone_Click(object sender, EventArgs e)
    {
        nowpages = 1;
        this.DataBd();
    }
    protected void toup_Click(object sender, EventArgs e)
    {
        --nowpages;
        this.DataBd();
    }
    protected void tonext_Click(object sender, EventArgs e)
    {
        ++nowpages;
        this.DataBd();
    }
    protected void tolast_Click(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(sumpage.Text);
        this.DataBd();
    }
    protected void jumppage_SelectedIndexChanged(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(((DropDownList)sender).SelectedValue);
        this.DataBd(); ;
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id= ((Label)GridView1.Rows[e.RowIndex].FindControl("id")).Text;
        WishingWallTable wwt = new WishingWallTable(Convert.ToInt32(id));
        try
        {
            wwt.Delete();
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('删除成功');</script>");
            this.DataBd();
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('删除失败');</script>");
        }
    }
    protected void delall_Click(object sender, EventArgs e)
    {
        int i=0;
        foreach (GridViewRow gv in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)gv.FindControl("CheckBox1");
            Label lb = (Label)gv.FindControl("id");
            if (cb.Checked)
            {
                i++;
                DBHandler dbh = GetHandler.Init();
                dbh.CommandText = string.Format("delete from News where id={0}", lb.Text);
                dbh.ExecuteNonQuery();
            }
        }
        if (i == 0)
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('请选择要删除的信息');</script>");
            this.DataBd();
            return;
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('删除成功');</script>");
            this.DataBd();
        }
       
    }
}